//
//  LYLStowViewController.m
//  TheLCNews
//
//  Created by dllo on 15/12/4.
//  Copyright © 2015年 LC. All rights reserved.
//

#import "LYLStowViewController.h"
#import "LCCoreDateManager.h"
#import "WWeb.h"
#import "LCInfoWebVC.h"

@interface LYLStowViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic ,strong) LCCoreDateManager *manager;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) UILabel *label;
@end

@implementation LYLStowViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    
    self.manager = [LCCoreDateManager defaults];
    [super viewDidLoad];
    
    // 创建查找请求,相当于数据库查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"WWeb"];
    
    // 添加一个约束 谓词
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@" , @"title", @"%@"],@"title"];
//    request.predicate = predicate;
    
    //通过某个 key 给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *arr = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    [self.array addObjectsFromArray:arr];
    NSLog(@"999%ld",self.array.count);
    
    [self creatView];
}

- (void)creatView
{
    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:0];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.tableview.scrollEnabled = NO;
    if (self.array.count < 1) {
        self.label = [[UILabel alloc] initWithFrame:self.view.bounds];
        self.label.hidden = NO;
        self.label.text = @"暂无收藏   ╮(╯▽╰)╭";
        self.label.textAlignment = 1;
        self.label.font = [UIFont systemFontOfSize:25];
        //label.backgroundColor = [UIColor purpleColor];
        [self.tableview addSubview:self.label];
    } else {
        self.label.hidden = YES;
    }

    [self.view addSubview:self.tableview];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    WWeb *model = self.array[indexPath.row];
    cell.textLabel.text = model.title;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCInfoWebVC *web = [[LCInfoWebVC alloc] init];
    WWeb *model = self.array[indexPath.row];
    web.dataUrl = model.dataUrl;
    [self.navigationController pushViewController:web animated:YES];
    //[self presentViewController:web animated:YES completion:^{
        
   
}

#pragma mark - tableView 编辑方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableview setEditing:editing animated:animated];
}

#pragma mark 是否允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// 4. 根据编辑状态, 增加或者删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    WWeb *model = self.array[indexPath.row];
  if (editingStyle == UITableViewCellEditingStyleDelete) {
//      NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"WWeb"];
//      
//      NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"title", @"%@"],model.title];
//      request.predicate = predicate;
//      NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
//      [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
//      
//      NSError *error;
      //self.array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
      
      for (NSInteger i = 0; i < self.array.count; i++) {
          if (i == indexPath.row) {
              [self.manager.managedObjectContext deleteObject:self.array[i]];
          }
      }
      
      
//      for (WWeb *wweb in self.array) {
//         
//          [self.manager.managedObjectContext deleteObject:wweb];
//      }
      [self.manager saveContext];
      
      // 创建查找请求,相当于数据库查询语句
      NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"WWeb"];
      
      // 添加一个约束 谓词
      //    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@" , @"title", @"%@"],@"title"];
      //    request.predicate = predicate;
      
      //通过某个 key 给数组排序
      NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
      [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
      
      NSError *error;
      
      NSArray *arr = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
      self.array = [NSMutableArray array];
      [self.array addObjectsFromArray:arr];
      NSLog(@"999%ld",self.array.count);

      
      [self.tableview reloadData];
      if (self.array.count < 1) {
          self.label = [[UILabel alloc] initWithFrame:self.view.bounds];
          self.label.hidden = NO;
          self.label.text = @"暂无收藏   ╮(╯▽╰)╭";
          self.label.textAlignment = 1;
          self.label.font = [UIFont systemFontOfSize:25];
          //label.backgroundColor = [UIColor purpleColor];
          [self.tableview addSubview:self.label];
      } else {
          self.label.hidden = YES;
      }

  }
}


// 删除的样式
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"取消收藏";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
